package logic

import (
	"api/model/mysql"
	"context"
	"goods_srv/cache"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartNumLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartNumLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartNumLogic {
	return &CartNumLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartNumLogic) CartNum(in *goods.CartNumRequest) (*goods.CartNumResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.NewGoods()
	err := g.GetDetail(int(in.GoodsId))
	if err != nil {
		logx.Error("商品信息查找失败")
		return nil, err
	}

	goodsMap := map[string]interface{}{
		"goods_id":     in.GoodsId,
		"goods_name":   g.GoodsName,
		"goods_price":  g.GoodsPrice,
		"goods_num":    in.GoodsNum,
		"goods_status": 1,
	}

	num := cache.CartNum(int(in.UserId), int(in.GoodsId))

	if num > 0 {
		goodsMap["goods_num"] = int(in.GoodsNum) + num
	}

	return &goods.CartNumResponse{
		Id: int64(g.ID),
	}, nil
}
